package bamin.com.bamindaijia.reciver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;

import com.aiton.administrator.shane_library.shane.utils.ACache;
import com.aiton.administrator.shane_library.shane.utils.GsonUtils;
import com.igexin.sdk.PushConsts;

import bamin.com.bamindaijia.Activity.MainActivity;
import bamin.com.bamindaijia.R;
import bamin.com.bamindaijia.constant.Constant;
import bamin.com.bamindaijia.model.Message;
import bamin.com.bamindaijia.model.NotificationInfo;
import bamin.com.bamindaijia.model.NotificationInfoShow;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by zjb on 2016/6/28.
 */
public class PushPassagerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_CLIENTID:

                String cid = bundle.getString("clientid");
                Log.e("onReceive ", "cid： "+cid);
                // TODO:处理cid返回
                break;
            case PushConsts.GET_MSG_DATA:
                Log.e("onReceive ", "有透传过来");
                String taskid = bundle.getString("taskid");
                Log.e("onReceive ", "taskid： "+taskid);
                String messageid = bundle.getString("messageid");
                Log.e("onReceive ", "messageid: "+messageid);
                byte[] payload = bundle.getByteArray("payload");
                if (payload != null) {
                    String data = new String(payload);
                    Log.e("onReceive ", "透传消息"+data);


                    Message message = GsonUtils.parseJSON(data, Message.class);
                    String title = message.getTitle();
                    Log.e("title",title);


                    String content = message.getContent();
                    String replace = content.replace("\'", "\"");
                    Log.e("content",replace);

                    if ("driverConfirmOrder".equals(title)) {
                        Log.e("PushPassagerReceiver", "driverConfirmOrder: --->>" +replace );
                        Intent intent1 = new Intent();
                        intent1.putExtra(Constant.INTENT_KEY.PUSH_ORDER_ING_VALUE,replace);
                        intent1.setAction(Constant.BROADCASTCODE.HAS_DRIVER);
                        context.sendBroadcast(intent1);
                    }else if ("driverArrival".equals(title)){
                        Intent intent1 = new Intent();
                        intent1.putExtra(Constant.INTENT_KEY.GETPASSAGER,replace);
                        intent1.setAction(Constant.BROADCASTCODE.GETPASSAGER);
                        context.sendBroadcast(intent1);
                    }else if ("getPassager".equals(title)){
                        Intent intent1 = new Intent();
                        intent1.putExtra(Constant.INTENT_KEY.GETPASSAGER,replace);
                        intent1.setAction(Constant.BROADCASTCODE.GETPASSAGER);
                        context.sendBroadcast(intent1);
                    }else if("driverChangeMileage".equals(title)){
                        Log.e("onMessage ", "content:"+replace);
                        Intent intent1 = new Intent();
                        intent1.putExtra(Constant.INTENT_KEY.DRIVER_CHANGE_MILEAGE,replace);
                        intent1.setAction(Constant.BROADCASTCODE.DRIVER_CHANGE_MILEAGE);
                        context.sendBroadcast(intent1);
                    }else if ("changeOrder".equals(title)){
                        Log.e("PushPassagerReceiver", "changeOrder: --->>" +replace );

                        Intent intent1 = new Intent();
                        intent1.putExtra(Constant.INTENT_KEY.CHANGE_END,replace);
                        intent1.setAction(Constant.BROADCASTCODE.CHANGE_END);
                        context.sendBroadcast(intent1);
                    }else if ("orderComplete".equals(title)){
                        Log.e("PushPassagerReceiver", "changeOrder: --->>" +replace );
                        Intent intent1 = new Intent();
                        intent1.putExtra(Constant.INTENT_KEY.ORDERCOMPLETE,replace);
                        intent1.setAction(Constant.BROADCASTCODE.ORDERCOMPLETE);
                        context.sendBroadcast(intent1);
                    }else if ("sendCoordinate".equals(title)){
                        Log.e("PushPassagerReceiver", "sendCoordinate: --->>" +replace );
                        Intent intent1 = new Intent();
                        intent1.putExtra(Constant.INTENT_KEY.SEND_COORDINATE,replace);
                        intent1.setAction(Constant.BROADCASTCODE.SEND_COORDINATE);
                        context.sendBroadcast(intent1);
                    }else if (TextUtils.equals("sendMessage",title)){
                        Log.e("onReceive ", "onReceive 通知 收到透传");
                        NotificationInfo notificationInfo = GsonUtils.parseJSON(replace, NotificationInfo.class);
                        ACache aCache = ACache.get(context,Constant.ACACHE.NOTIFICATION);
                        aCache.put(Constant.ACACHE.NOTIFICATIONINFO,new NotificationInfoShow(notificationInfo,false));
                        Intent intent3 = new Intent();
                        intent3.putExtra(Constant.BROADCASTCODE.NOTIFICATIONINFO,notificationInfo);
                        intent3.setAction(Constant.BROADCASTCODE.NOTIFICATIONINFO);
                        context.sendBroadcast(intent3);

                        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
                        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                        RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.notification_view);
                        views.setImageViewResource(R.id.img_icon,R.mipmap.daijia);
                        views.setTextViewText(R.id.text_title,notificationInfo.getMessageTitle());
                        views.setTextViewText(R.id.text_content,notificationInfo.getMessageText());
                        builder.setContent(views);
                        builder.setSmallIcon(R.mipmap.daijia);
                        Intent intent1 = new Intent();
                        intent1.setClass(context,MainActivity.class);
                        intent1.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        PendingIntent intent2= PendingIntent.getActivity(context, 0, intent1, 0);
                        builder.setContentIntent(intent2);
                        Notification build = builder.build();
                        build.flags= Notification.FLAG_AUTO_CANCEL;
                        manager.notify(1028, build);
                    }

                }
                break;
            default:
                break;
        }
    }
//    Log.e("onMessage ", "乘客onMessage "+s);
//    Message message = GsonUtils.parseJSON(s, Message.class);
//    String title = message.getTitle();

}
